  var _USER_NAME_REGEX =  /^([a-zA-Z0-9]){0,20}$/;

  var _USER_NAME_UNWANTED_CHARACTER_REGEX = /[^a-zA-Z0-9]/g;
  var _CURRENT_PAGE="_currentPage";
  var _PAGE_SIZE="_pageSize";
  var parameters=null;
  //check the result returned by ajax invoking. 
  //If session is time out, -1 returned.
  function isTimeout(data,hasParent){
	  if(data == -1){
		  redirectToLogin(hasParent);
		  return true;
	  }
	  return false;
  }

  //reload page to enforce spring security to intercept url 
  // and redirect to login page
  function redirectToLogin(hasParent){
	    if (hasParent) {
			parent.location.reload();
			self.parent.tb_remove();
		} else {
			window.location.reload();
		}
  }
  
  //check input text with out special signal
  function isCharValid(val){
	 var patrn = new   RegExp("[\|~`!@#\$%\^&\*\<\>\{\}()\+\-]+");
	 if(!val) {
	     return true;
	 }
	 if (val.match(patrn) == null) {
	     return true;
	 }else 
     {
	     return false;
	 }
  }

  function isPasswordValid(val){
	     var patrn = new   RegExp("[~`\^\*\<\>;\'\"]+");
		 if(!val) {
		     return true;
		 }
		 if (val.match(patrn) == null) {
		     return true;
		 }  else 
	     {
		     return false;
		 }
  }
	
	function nonspecial(pig) {
		
		var keyCode = window.event.keyCode;
		if(keyCode=="37"||keyCode=="39"){
			return true;
		}
		var s = pig.value;
		
		if (s && s != '') {
		    pig.value = s.replace(/[\"\'\\\\~`!@#\$%\^&\*\<\>\{\}\+\-]+/g, '');
		    
		    return true;
		}
		
		return false;
		/*
		var val = pig.value;
		var patrn = new RegExp("[\|~`!@#\$%\^&\*\<\>\{\}\+\-]+");
		
		if(!val) {
	                	return true;
		}
		if (val.match(patrn) == null) {
		return true;
		}  else {
		   pig.value=pig.value.substr(0,pig.value.length-1);
		   nonspecial(pig);
		}
		*/
	}

	function checkPwd(obj){
		if(obj.value.length > 20){
			alert("密码最多20个字符，请重新输入。");
			return false;
		}
		if(obj.value.length < 1){
			alert("请输入密码。");
			return false;
		}
		return true;
	}

    function replaceInvalidCharForUserName(obj){
        value = obj.value;
        if(value == ''){
            return;
        }
		obj.value = value.replace(_USER_NAME_UNWANTED_CHARACTER_REGEX,'');
	}

    function encodePassword(obj){
		obj.value = encodeURIComponent(obj.value);
	}

	function checkUserName(obj){
		if(!_USER_NAME_REGEX.exec(obj.value)){
			alert("输入字符非法，只能输入20个英文字母或数字，请重新输入。");
			return false;
		}
		if(obj.value < 1){
			alert("请输入用户帐号。");
			return false;
		}
		return true;
	}

//	function nonspecialremark(tt) {		
//		var val = tt.value;
//		var patrn = new   RegExp("[\~`;'\"\^\*\<\>\]");
//		if(!val) {
//	        return true;
//		}
//		if (val.match(patrn) == null) {
//			return true;
//		}  else {
//		   tt.value=tt.value.substr(0,tt.value.length-1);
//		   nonspecialremark(tt);
//		}	
//	}
	
	function getPageSize(index){
		var pageSize=parseInt($("#pageSize_"+index).val());
		return pageSize;
	}
	//pagination
	//go to the first page
	function gotoFirst(index){
		var currentPage = 1;
		submit(currentPage,index);
	}
	//go to previous page
	function gotoPrev(index){
		var currentPage = parseInt($("#_p_currentPage_id"+index).val()) - 1;
		if(currentPage < 1){
			currentPage = 1;
		}
		submit(currentPage,index);
	}
	//go to specified page
	function directToPage(event,index){
		if(event.keyCode == 13) {
			var currentPage = $("#_p_gotoPage_id"+index).val();
			//check input 
			currentPage = trim(currentPage);
			var pattern = /^[1-9][0-9]*$/; 
            event.returnValue = false;
			if (!pattern.exec(currentPage)) {
				alert("输入的页码不正确");
				return false;
			}
			//check max
			var totalPage = parseInt($("#_p_totalPage_id"+index).val());
			currentPage = parseInt(currentPage);
			if(currentPage > totalPage){
				currentPage = totalPage;
			}
			submit(currentPage,index);
		}
		
	}
	//go to next page
	function gotoNext(index){
		var currentPage = parseInt($("#_p_currentPage_id"+index).val()) + 1;
		var totalPage = parseInt($("#_p_totalPage_id"+index).val());
		if(currentPage > totalPage){
			currentPage = totalPage;
		}
		//alert(currentPage);
		submit(currentPage,index);
	}
	//go to the last page
	function gotoLast(index){
		var currentPage = parseInt($("#_p_totalPage_id"+index).val());
		submit(currentPage,index);
	}
	//refresh current page
	function refresh(index){
		var currentPage = parseInt($("#_p_currentPage_id"+index).val());
		submit(currentPage,index);
	}
	//submit form
	function submit(currentPage,index){
		var action = $("#_p_action_id"+index).val();
		$("#_p_currentPage_id"+index).val(currentPage);
		var method=$('#_p_method_id'+index).val().toLowerCase();
		var ajaxFunc=$('#_p_ajaxFunc_id'+index).val();
		if(ajaxFunc!=null&&ajaxFunc!='undefined'&&ajaxFunc!=''){
			var pageSize=getPageSize(index);
			parameters={'_currentPage':currentPage,'_pageSize':pageSize};
			//ajaxFunc(parameters);
			invokeFunction2(ajaxFunc,'parameters');
		}
		else{
			var formName = $("#_p_form_id"+index).val();
			var targetForm = $(eval('document.'+formName));
			targetForm.attr("action", action);
			if(method!='undefined'&&method!=''){
				targetForm.attr("method",method);
			}
			
			var validateFuncName=$("#_p_validateFunc_id"+index).val();
			//alert(validateFuncName);
			if(validateFuncName!=null&&validateFuncName!='undefined'&&validateFuncName!=''){
				if(!eval(validateFuncName+'()')){
					return;
				}
			}
			targetForm.submit();
		}
	}
	
	//changePageSize
	function changePageSize(index){
		var pageSize=getPageSize(index);
		$("#_p_pageSize_id"+index).val(pageSize);
		submit(1,index);
	}
	
	function invokeFunction2(func,paras){
		var invokeStr=func+"(";
		if(paras){
			invokeStr+=paras;
		}
		invokeStr+=")";
		eval(invokeStr);
	}
	
	function trim(str) 
	{ 
	   return str.replace(/(^\s*)|(\s*$)/g, ""); 
	} 
	
	function testJawr(str){
		alert(str);
	}	
	
	//disable mouse right click
	function nocontextmenu()
	{
		/*event.cancelBubble = true;
		event.returnValue = false;
		return false;*/
		return true;
	} 

	$(document).ready(function(){
		$('.sortable>tbody>tr').hover(
		  function(){
		   $(this).addClass('MouseOver');
		  },
		  function() 
		  {
		   $(this).removeClass('MouseOver');
		  }
		);
		this.oncontextmenu = nocontextmenu;
	});
	
	/**
	 * Check invalid char for part id, part name
	 */
	function isValidPartIdName(val) {
		var patrn = new RegExp("[\"\'\\\\~`!@#\^&\*\<\>\{\}\+\-]+");
		if (!val) {
		    return true;
		}
		if (val.match(patrn) == null) {
		    return true;
		} else {
		    return false;
		}
	}